package bairui.international.weixin.portal.miniapp.controller.product.product;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.model.domain.jpa.TB_ProductPropertyItem;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.domain.jpa.TB_UserAmount;
import bairui.international.service.Service_Product;
import bairui.international.service.Service_ProductPropertyItem;
import bairui.international.service.Service_User;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 得到产品
 *
 * @generated
 * 
 * @see STest_C_Product_Get_Get 测试
 *
 */
@Controller
public class C_Product_Get_Get
 extends SC_Product_Get_Get 
{

	@Autowired private Service_Product service_Product;
	@Autowired private Service_User service_User;
	@Autowired private Service_ProductPropertyItem service_ProductPropertyItem;

	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request, 
		@PathVariable(name=Get.PathVariableName.Id) long id,
		@RequestParam String skey
	) throws Exception {
		TB_Product bean = service_Product.findById(id);
		//
		return new Output(bean, skey, service_User, service_ProductPropertyItem);
	}
	
	@AllArgsConstructor
	private static class Output{
		@Getter private Long rootCategoryId;	
		@Getter private String rootCategoryName;	
		@Getter private Long id;
		@Getter private String name;
		@Getter private String comment;
		@Getter private Double price;
		@Getter private Double showPrice;
		@Getter private Double score;
		@Getter private String detail;
		@Getter private String imgUrl;
		@Getter private Double totalScore;	// 可易物总积分
		@Getter private String unit = "";
		
		public Output(TB_Product bean, String skey, Service_User service_User, Service_ProductPropertyItem service_ProductPropertyItem){
			// 处理易物积分
			TB_User user = service_User.findOne(it->it.skey.eq(skey));
			TB_UserAmount grade = user.getUserAmount();
			Double totalScore = null == grade ? 0D : grade.getGrade();
			/*
			 * 
			 */
			this.rootCategoryId = bean.getRootCategory().getId();
			this.rootCategoryName = bean.getRootCategory().getName();
			this.id = bean.getId();
			this.name = bean.getName();
			this.comment = null == bean.getComment() ? "" : bean.getComment();
			this.price = bean.getPrice();
			this.showPrice = bean.getShowPrice();
			this.score = bean.getScore();
			this.detail = bean.getDetail();
			this.imgUrl = new C_Product_ShowImg_Get.Get.Operate() {
				@Override public Object getId() {
					return bean.getId();
				}
			}.getUrl();
			this.totalScore = totalScore;
			//
			TB_ProductPropertyItem p = service_ProductPropertyItem.findOne(it->
				it.productProperty.name.eq("单位").and(it.products.any().eq(bean))
			);
			if(null != p){
				this.unit = p.getName();
			}
		}
		
	}

}
